package process

import (
	"../../common/message"
	"net"
	"encoding/json"
	"fmt"
	"../../common/utils"
)

type SmsProcess struct {
}

func (obj * SmsProcess) SendGroupMes(msg *message.Message) {

	var smsMsg message.SmsMes
	err := json.Unmarshal([]byte(msg.Data), &smsMsg)
	if err != nil {
		fmt.Println("smsMsg json unmarshal err = ", err)
		return
	}

	data, err := json.Marshal(msg)
	if err != nil {
		fmt.Println("json.Marshal err= ", err)
		return
	}

	for uid, up := range userMgr.onlineUsers {
		if uid == smsMsg.UserId {
			continue
		}
		obj.SendMsgToEachOnlineUser(data, up.Conn)
	}
}

func (obj * SmsProcess) SendMsgToEachOnlineUser(data []byte, conn net.Conn) {

	tf := &utils.Transfer{
		Conn: conn,
	}

	err := tf.WritePkg(data)

	if err != nil {
		fmt.Println("转发消息失败 writePkg error =", err)
		return
	}
}